import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import { PreferenceService, WidgetManager } from '@theia/core/lib/browser';
import { AbstractViewContribution } from '@theia/core/lib/browser';
import { CommandRegistry, Command, MenuModelRegistry } from '@theia/core';
import { EcsShellWidget } from './ecs-shell-widget';

export const EcsShellCommand: Command = { id: 'ecs-shell-widget:command' };

/**
 * Contribution of the `EcsShellWidget`
 */
@injectable()
export class EcsShellViewContribution extends AbstractViewContribution<EcsShellWidget> {
    @inject(PreferenceService) protected readonly preferenceService: PreferenceService;
    @inject(MenuModelRegistry) protected menuModel: MenuModelRegistry;
    @inject(WidgetManager) protected readonly widgetManager: WidgetManager;

    constructor() {
        super({
            widgetId: EcsShellWidget.ID,
            widgetName: EcsShellWidget.LABEL,
            defaultWidgetOptions: { 
                area: 'left',
                rank: 1  // 设置较高的优先级
            },
            toggleCommandId: EcsShellCommand.id,
        });
    }

    @postConstruct()
    protected init(): void {
        this.doInit();
    }

    protected async doInit(): Promise<void> {
        try {
            await this.preferenceService.ready;
            await this.shell.initialized;
        } catch (error) {
            console.error('Failed to initialize EcsShellViewContribution:', error);
        }

        // 触发当前widget 初始显示
        super.openView({ activate: true, reveal: true });
    }

    async onStart(): Promise<void> {
        console.log('---------------onstart')

    }

    registerMenus(menus: MenuModelRegistry): void {
        super.registerMenus(menus);
    }

    registerCommands(commands: CommandRegistry): void {
        commands.registerCommand(EcsShellCommand, {
            execute: () => super.openView({ activate: false, reveal: true })
        });
    }
}
